﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Edit_Maker : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public string maker_id;

        public frm_Edit_Maker()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.maker_id = null;            
        }

        //////////////////////////////////////////////
        /// Load data
        //////////////////////////////////////////////
        public bool LoadData()
        {
            bool ret = true;

            if (string.IsNullOrEmpty(maker_id))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand cmd_sql = myConnection.CreateCommand();
                    cmd_sql.CommandText = "SELECT * FROM [222_Maker] WHERE MakerID='" + maker_id + "'";
                    SqlDataReader dataReader = cmd_sql.ExecuteReader();
                    if (dataReader.Read())
                    {
                        this.makerID.Text = maker_id;
                        this.makerName.Text = dataReader["MakerName"].ToString();
                        this.englishName.Text = dataReader["EnglishName"].ToString();                        
                    }
                    else
                    {
                        ret = false;
                    }
                    dataReader.Close();
                    cmd_sql.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }

            return ret;
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string maker_name = null;
            string english_name = null;                        
                       

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.makerName.Text))
                {
                    error = "Please enter Maker Name";
                }
                else
                {
                    maker_name = this.makerName.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.englishName.Text))
                {
                    error = "Please enter English Name";
                }
                else
                {
                    english_name = this.englishName.Text;
                }
            }
            
            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "UPDATE [222_Maker] SET MakerName=N'" + maker_name + "',"
                        + " EnglishName=N'" + english_name + "'"
                        + " WHERE MakerID='" + maker_id + "'";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Maker updated success.");
                    this.Dispose();                    
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                    //MessageBox.Show("Cannot update Maker. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }
    }
}
